package model{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.text.*;
	
	import model.GlobalTextFormat;
	
	public class CustomTitleBar extends Sprite
	{
		
		private var titleBar:TitleBarMovieClip;
		private var titleLabel:TextField;
		public var navButton:NavigationButton;
		public var actionButton:ActionButton;
		private var gap:int = 6;
		
		public function CustomTitleBar()
		{
			titleBar = new TitleBarMovieClip();
			titleLabel = new TextField();
			navButton = new NavigationButton();
			actionButton = new ActionButton();
			addChild(titleBar);
			addChild(navButton);
			addChild(actionButton);
			actionButton.visible = false;
			navButton.visible = false;
			actionButton.x = 7;
			actionButton.y = gap;
			actionButton.addEventListener(MouseEvent.CLICK, actionButtonHandler);
			navButton.addEventListener(MouseEvent.CLICK, navButtonHandler);
		}
		
		private function actionButtonHandler(evt:MouseEvent):void
		{
			this.dispatchEvent(new Event("ACTION_BUTTON_SELECTED",true));
		}
		
		private function navButtonHandler(evt:MouseEvent):void
		{
			this.dispatchEvent(new Event("NAV_BUTTON_SELECTED",true));
		}
		
		public function set title(value:String):void
		{
			if(!contains(titleLabel))
			{				
				titleLabel.selectable = false;
				titleLabel.multiline = true; 
				titleLabel.autoSize = TextFieldAutoSize.CENTER;
				addChild(titleLabel);
			}			
			//label.border = true;
			titleLabel.text = value;			
			titleLabel.y = gap;
			titleLabel.setTextFormat(GlobalTextFormat.titleAssetStyle);
		}
		
		override public function set width(value:Number):void
		{
			titleLabel.x = 0;
			titleBar.width = value;
			titleLabel.width = value;
			navButton.x = titleBar.width - navButton.width - 7;
			navButton.y = gap;
		}
		
		override public function get width():Number
		{
			return titleBar.width;
		}
		
		override public function set height(value:Number):void
		{
			titleBar.height = value;
		}
		
		override public function get height():Number
		{
			return titleBar.height;
		}
	}	
}